package com.nf.practice.retrytest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import com.google.common.base.Predicates;
import netscape.javascript.JSObject;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

public class RetryImpl {

    @Test
    public void test2() {
        JSONObject s1 = new JSONObject();
        s1.put("number", "1");
        JSONObject s2 = null;
        JSONObject s3 = null;
        Args(s1, s2, s3);
    }

    public void Args(JSONObject... arrays) {
        JSONObject jsonObject1 = null;
        jsonObject1 = arrays[0];
        System.out.println(jsonObject1);
    }

    @Test
    public void test() {
        System.out.println("开始发短信");
        String res = sendSMS("10086", "开发组");
        System.out.println("结果是：" + res);
    }


    public String sendSMS(final String phone, final String content) {
        Retryer<String> retryer = RetryerBuilder.<String>newBuilder()
                .retryIfResult(Predicates.equalTo("2")) // 返回false时重试
                .retryIfExceptionOfType(IOException.class) // 抛出IOException时重试
                .withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS)) // 2s后重试
                .withStopStrategy(StopStrategies.stopAfterAttempt(4)) // 重试3次后停止
                .build();
        try {
            return retryer.call(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    System.out.println("实现call方法");
                    return doSomething(phone, content);
                }
            });
        } catch (Exception e) {
            return "3";
        }
    }

    // 给某人发短信
    private String doSomething(String phone, String content) {
        System.out.println("开始执行doSomething()");
//        int i = 10 / 0;
        return "2";
    }

}
